<?php

namespace app\xcxapi\service;

use app\common\service\BaseService;
use app\lib\exception\BaseException;
use app\models\AllocationCampusModel;
use app\models\AllocationOnlyModel;
use app\models\CampusModel;
use app\models\ContractModel;
use app\models\GradesDetailsModel;
use app\models\MyApprovalModel;
use app\models\MyApprovalRelationModel;
use app\models\MyNoticeModel;
use app\models\MyNoticeObjectModel;
use app\models\ParentInformationModel;
use app\models\StudentModel;
use app\models\StudentSubjectInfosClassModel;
use app\models\UserModel;
use think\Db;

class MyService extends BaseService
{
    /**
     * 通知列表
     * User: wufeng
     * Date: 2020/03/07.
     *
     * @param $params
     *
     * @return array
     */
    public function noticeLists($params)
    {
        try {
            $where = [];
            $where['user_id'] = xcxapi_get_current_user_id();
            if (4 === (int) $params['status']) {
                $where['status'] = ['in', '1,3']; //存稿还是发布：1发布 2存稿 3撤销
            } else {
                $where['status'] = $params['status']; //存稿还是发布：1发布 2存稿 3撤销
            }
			
			$notice_id_array =(new MyNoticeObjectModel())->where(['n_type' => 1])->column('notice_id'); //通知ID
			$notice_id_array = array_unique($notice_id_array);
			$where['id'] = ['in', implode(',',$notice_id_array )];
			
            $this->data['notice'] = (new MyNoticeModel())
                ->where($where)
                ->order('id desc')
                ->select()
                ->toArray();

            foreach ($this->data['notice'] as &$val) {
                $val['name'] = (new UserModel())->get($val['user_id'])['name'];
                $notices_array = explode(',', $val['notice_object']);
                $data_ = [];
				$already_read_count = (new MyNoticeObjectModel())->where(['notice_id' => $val['id'], 'is_read' => 1])->count();
				$not_read_count = (new MyNoticeObjectModel())->where(['notice_id' => $val['id'], 'is_read' => 0])->count();
                $val['read_status'] = $already_read_count.'/'.$not_read_count ;
				foreach ($notices_array as &$item) {
                    $cell = [];
                    $cell['id'] = $item;
					$n_type = (new MyNoticeObjectModel())->get(['notice_id' => $val['id'], 'notice_object_id' => $item])['n_type'];
					if($n_type == 1){  //家长端
							$student_id = (new ParentInformationModel())->get($item)['student_id'];
							$cell['name'] = (new StudentModel())->get($student_id)['name'];
					}else if($n_type == 2){ //机构端
							$cell['name'] = (new UserModel())->get($item)['name'];
					}
                    $data_[] = $cell;
                }
                $val['notice_user'] = $data_;
            }
            $this->data['draft_num'] = (new MyNoticeModel())
                ->where(['user_id' => xcxapi_get_current_user_id(), 'status' => 2])
                ->count();
        } catch (\Exception $e) {
            $this->errorMessage($e, $params);
        }

        return $this->return;
    }

    /**
     * User: wufeng
     * Date: 2020/03/07.
     *
     * @param $params
     *
     * @return array
     */
    public function notice($params)
    {
        try {
            $data = [];
            $data['notice_time'] = date('Y-m-d H:i:s'); //通知时间
            $data['user_id'] = xcxapi_get_current_user_id(); //发布人id
			/*
            $data['notice_object'] = $params['notice_object']; //通知对象
			*/
			$data_user = $params['notice_object_user']; //通知对象(非学生)
			$data_student = $params['notice_object_student']; //通知对象(学生)
			if($data_user == null || $data_user == ''){
				$data['notice_object'] = $data_student;
			}else if($data_student == null || $data_student == ''){
				$data['notice_object'] = $data_user;
			}else if($data_user != null && $data_user != '' && $data_student != null && $data_student != ''){
				$data['notice_object'] = $data_user.','.$data_student ;
			}
			
			
            $data['notice_name'] = $params['notice_name']; //通知名称
            $data['content'] = $params['content']; //具体内容
            $data['audio'] = $params['audio']; //音频
            $data['audio_duration'] = $params['audio_duration']; //音频时长
            $data['video'] = $params['video']; //视频
            $data['picture'] = $params['picture']; //图片
            $data['notice_type'] = $params['notice_type']; //通知方向：1机构端、家长端；2机构端；3家长端
            $data['status'] = $params['status']; //存稿还是发布：1发布 2存稿
            if (isset($params['id']) && '' !== $params['id'] && null !== $params['id']) {
                Db::name('edu_my_notice')->where(['id' => $params['id']])->update($data);
                Db::name('edu_my_notice_object')->where(['notice_id' => $params['id']])->delete();
                
				//非学生
				$notices_users = [];
				if($data_user != '' && $data_user != null){
				$notices_users = explode(',', $data_user);
				}
				$data_user = [];
                foreach ($notices_users as &$val) {
                    $item = [];
                    $item['notice_id'] = $params['id'];
                    $item['notice_object_id'] = $val;
					$item['notice_type'] = 1;
                    $data_user[] = $item;
                }
				if(count($data_user) > 0){
                Db::name('edu_my_notice_object')->insertAll($data_user);
				}
				
				//学生
				$notices_student = [];
				if($data_student != '' && $data_student != null){
				$notices_student = explode(',', $data_student);
				}
				$data_student = [];
                foreach ($notices_student as &$val) {
                    $item = [];
                    $item['notice_id'] = $params['id'];
                    $item['notice_object_id'] = $val;
					$item['notice_type'] = 1;
                    $data_student[] = $item;
                }
				if(count($data_student) > 0){
                Db::name('edu_my_notice_object')->insertAll($data_student);
				}

            //插入
            } else {
                Db::name('edu_my_notice')->insert($data);
                $id = Db::name('edu_my_notice')->getLastInsID();
				
               	//非学生
				$notices_users = [];
				if($data_user != '' && $data_user != null){
				$notices_users = explode(',', $data_user);
				}
                $data_user = [];
                foreach ($notices_users as &$val) {
                    $item = [];
                    $item['notice_id'] = $id;
                    $item['notice_object_id'] = $val;
					$item['n_type'] = 2;
					$item['notice_type'] = 1;
                    $data_user[] = $item;
                }
				if(count($data_user) > 0){
                Db::name('edu_my_notice_object')->insertAll($data_user);
				}
				
				//学生
				$notices_student = [];
				if($data_student != '' && $data_student != null){
				$notices_student = explode(',', $data_student);
				}
                $data_student = [];
                foreach ($notices_student as &$val) {
                    $item = [];
                    $item['notice_id'] = $id;
                    $item['notice_object_id'] = $val;
					$item['n_type'] = 1;
					$item['notice_type'] = 1;
                    $data_student[] = $item;
                }
				if(count($data_student) > 0){
					Db::name('edu_my_notice_object')->insertAll($data_student);
				}
            }
        } catch (\Exception $e) {
            $this->errorMessage($e, $params);
        }

        return $this->return;
    }

    /**
     * User: wufeng
     * Date: 2020/03/07.
     *
     * @param $params
     *
     * @return array
     */
    public function noticeDel($params)
    {
        try {
            (new MyNoticeModel())->where(['id' => $params['id']])->delete();
            (new MyNoticeObjectModel())->where(['notice_id' => $params['id']])->delete();
        } catch (\Exception $e) {
            $this->errorMessage($e, $params);
        }

        return $this->return;
    }

    /**
     * User: wufeng
     * Date: 2020/03/13.
     *
     * @param $params
     *
     * @return array
     */
    public function noticeCancel($params)
    {
        try {
            (new MyNoticeModel())->save(['status' => 3], ['id' => $params['id']]);
        } catch (\Exception $e) {
            $this->errorMessage($e, $params);
        }

        return $this->return;
    }

    /**
     * User: wufeng
     * Date: 2020/03/07.
     *
     * @param $params
     *
     * @return array
     */
    public function approval($params)
    {
        try {
            $where = $data = [];
			$approval = (new MyApprovalModel())->get($params['id']);
            $where['approver_id'] = xcxapi_get_current_user_id();
            $where['approval_id'] = $params['id'];
            $data['status'] = $params['status'];
            $data['approval_content'] = $params['approval_content'];
            $data['approval_time'] = date('Y-m-d H:i:s');
            Db::name('edu_my_approval_relation')->where($where)->update($data);
			$flag = 1;
			if($approval['type'] == 1){  //同事
					$flag = 2;
			}else if($approval['type'] == 2){  //学生
					$flag = 1;
			}
            $check = Db::name('edu_my_approval_relation')->where(['approval_id' => $params['id'], 'status' => 3])->find();
            if ($check) {
                Db::name('edu_my_approval')->where(['id' => $params['id']])->update(['status' => 3]);
                CommonService::writeNotice($approval['user_id'],$approval['approval_name'],$approval['approval_name'].'审批'.'已被驳回', $flag, 2);
            }

            $my_approval = Db::name('edu_my_approval')->where(['id' => $params['id']])->find();
            $student_id = (new ParentInformationModel())->get($my_approval['user_id'])['student_id'];
            $my_approval_count = Db::name('edu_my_approval_relation')->where(['approval_id' => $params['id']])->count();
            $my_approval_status_count = Db::name('edu_my_approval_relation')->where(['approval_id' => $params['id'], 'status' => 2])->count();
            if ($my_approval_count === $my_approval_status_count) {
                Db::name('edu_my_approval')->where(['id' => $params['id']])->update(['status' => 2]);
                Db::name('edu_class_subject_student_infos')->where(['class_manage_details_id' => $my_approval['cmdl_id'], 'student_id' => $student_id])->update(['attendance_reasons' => $my_approval['approval_name'], 'remarks' => $my_approval['approval_content']]);
				CommonService::writeNotice($approval['user_id'],$approval['approval_name'],$approval['approval_name'].'审批'.'已被通过', $flag, 2);
            }
        } catch (\Exception $e) {
            $this->errorMessage($e, $params);
        }

        return $this->return;
    }

    /**
     * User: wufeng
     * Date: 2020/03/07.
     *
     * @param $params
     *
     * @return array
     */
    public function launchApproval($params)
    {
        try {
            $data = [];
            $data['approval_time'] = date('Y-m-d H:i:s'); //审批时间
            $data['user_id'] = xcxapi_get_current_user_id(); //发布人id
            $data['approval_name'] = $params['approval_name']; //审批名称
            $data['content'] = $params['content']; //具体内容
            $data['picture'] = $params['picture']; //图片
            $data['approver'] = $params['approver']; //审批人
            $data['status'] = 1;
            $data['type'] = $params['type'];
            Db::name('edu_my_approval')->insert($data);
            $id = Db::name('edu_my_approval')->getLastInsID();
            $_data = [];
            $approver_ids = explode(',', $params['approver']);
            foreach ($approver_ids as &$approver_id) {
            	$approver = [];
				$approver['approval_id'] = $id;
				$approver['approver_id'] = $approver_id;
				$approver['status'] = 1;
				$_data[] = $approver;
            }
			Db::name('edu_my_approval_relation')->insertAll($_data);
            //写入通知记录
            CommonService::writeNotice($params['approver'],$params['approval_name'],'收到了'.xcxapi_get_current_name().$params['approval_name'].'审批申请',2, 1);
        } catch (\Exception $e) {
            $this->errorMessage($e, $params);
        }

        return $this->return;
    }

    /**
     * 审批列表
     * User: wufeng
     * Date: 2020/03/07.
     *
     * @param $params
     *
     * @return array
     */
    public function approvalLists($params)
    {
        try {
            //1待审批 2已审批 3已驳回
            $where = $_where = [];
            $where['status'] = 1;
            $ids = (new MyApprovalRelationModel())->where(['approver_id' => xcxapi_get_current_user_id()])->column('approval_id');
            $where['id'] = ['in', implode(',', $ids)];
			$where['operation_type'] = 1; //审批类
            $where['user_id'] = ['neq', xcxapi_get_current_user_id()];
            $this->data['stay_approval'] = (new MyApprovalModel())
                ->where($where)
				->order('id desc')
                ->select()
                ->toArray();
            foreach ($this->data['stay_approval'] as &$datum) {
                $datum['name'] = (new UserModel())->get($datum['user_id'])['name'];
            }

            $_where['status'] = ['neq', 1];
			$_where['operation_type'] = 1; //审批类
            $_where['user_id'] = ['neq', xcxapi_get_current_user_id()];
            $_where['id'] = ['in', implode(',', $ids)];
            $this->data['already_approval'] = (new MyApprovalModel())
                ->where($_where)
				->order('id desc')
                ->select()
                ->toArray();
            foreach ($this->data['already_approval'] as &$datum) {
                $datum['name'] = (new UserModel())->get($datum['user_id'])['name'];
            }
            $this->data['own_already_approval'] = (new MyApprovalModel())
                ->where(['user_id' => xcxapi_get_current_user_id(), 'operation_type' => 1, 'status' => ['in', '2,3']])
				->order('id desc')
                ->select()
                ->toArray();
            foreach ($this->data['own_already_approval'] as &$datum) {
                $datum['name'] = (new UserModel())->get($datum['user_id'])['name'];
                unset($datum['type']);
                $datum['type'] = 3;
            }

            $this->data['own_stay_approval'] = (new MyApprovalModel())
                ->where(['user_id' => xcxapi_get_current_user_id(), 'operation_type' => 1, 'status' => ['in', '1,4']])
				->order('id desc')
                ->select()
                ->toArray();
            foreach ($this->data['own_stay_approval'] as &$datum) {
                $datum['name'] = (new UserModel())->get($datum['user_id'])['name'];
                unset($datum['type']);
                $datum['type'] = 3;
            }
        } catch (\Exception $e) {
            $this->errorMessage($e, $params);
        }

        return $this->return;
    }

    /**
     * User: wufeng
     * Date: 2020/03/08.
     *
     * @param $params
     *
     * @return array
     */
    public function approvalDetail($params)
    {
        try {
            $this->data['detail'] = (new MyApprovalModel())->get($params['id']);
            $this->data['lists'] = (new MyApprovalRelationModel())
                ->where(['approval_id' => $params['id']])
				->order('id desc')
                ->select()
                ->toArray();
            foreach ($this->data['lists'] as &$list) {
                $list['name'] = (new UserModel())->get($list['approver_id'])['name'];
            }
        } catch (\Exception $e) {
            $this->errorMessage($e, $params);
        }

        return $this->return;
    }

    /**
     * User: wufeng
     * Date: 2020/03/07.
     *
     * @return array
     */
    public function approverLists()
    {
        try {
            $where = $class = [];
            $where['company_id'] = xcxapi_get_current_company_id();
            $where['id'] = ['neq', xcxapi_get_current_user_id()];  //排除自己
            $this->data['staff'] = (new UserModel())
                ->where($where)
                ->where(['wtype' => 5])
				->order('id desc')
                ->select()
                ->toArray();
            $this->data['campus'] = (new UserModel())
                ->where($where)
                ->where(['wtype' => 4])
				->order('id desc')
                ->select()
                ->toArray();
            $this->data['regional'] = (new UserModel())
                ->where($where)
                ->where(['wtype' => 3])
				->order('id desc')
                ->select()
                ->toArray();
            $this->data['company'] = (new UserModel())
                ->where($where)
                ->where(['wtype' => 2])
				->order('id desc')
                ->select()
                ->toArray();
            $this->data['wtype'] = xcxapi_get_current_wtype();
            $this->data['campus_arr'] = (new CampusModel())
                ->where(['id' => ['in', xcxapi_get_current_campus_id()]])
				->order('id desc')
                ->select()
                ->toArray();

            foreach ($this->data['campus_arr'] as &$datum) {
                $datum['gradesDetails'] = (new GradesDetailsModel())
                    ->where(['campus_id' => $datum['id'], 'graduation_status' => 0])
                    ->select()
                    ->toArray();
                foreach ($datum['gradesDetails'] as &$val) {
                    $student_ids = (new StudentSubjectInfosClassModel())->where(['class_subject_id' => $val['id']])->column('student_id');
                    $val['student'] = array_values(array_unique($student_ids));
                    $array_srudent = [];

                    for ($i = 0; $i < \count($val['student']); ++$i) {
                        $array_srudent[$i]['student_id'] = $val['student'][$i];
                    }
                    $val['student'] = $array_srudent;

                    foreach ($val['student'] as &$item) {
                        $student = (new StudentModel())->get($item['student_id']);
                        $item['student_name'] = $student['student_name'];
                        $item['student_phone'] = $student['phone'];
                        $item['student_user_id'] = (new ParentInformationModel())->get(['student_id' => $item['student_id'], 'username' => $student['guardian_phone']])['id'];
                    }
                }
            }
        } catch (\Exception $e) {
            $this->errorMessage($e, '');
        }

        return $this->return;
    }

    /**
     * User: wufeng
     * Date: 2020/03/08.
     *
     * @param $params
     *
     * @return array
     */
    public function cancel($params)
    {
        try {
            (new MyApprovalModel())->save(['status' => 4], ['id' => $params['id']]);
			$where['approver_id'] = xcxapi_get_current_user_id();
			$where['approval_id'] = $params['id'];
			$data['status'] = 4;
			$data['approval_time'] = date('Y-m-d H:i:s');
			Db::name('edu_my_approval_relation')->where($where)->update($data);
        } catch (\Exception $e) {
            $this->errorMessage($e, $params);
        }

        return $this->return;
    }

    /**
     * User: chenben
     * Date: 2020/03/09.
     *
     * @param $params
     *
     * @return array
     */
    public function achievementdetail($params)
    {
        try {
            $where = [];
            $date = date('Y-m-d');
            $start = $date.' 00:00:00';
            $end = $date.' 23:59:59';

            //今日签单数
            $where['signing_date'] = $date;
            if (4 === xcxapi_get_current_wtype() || 5 === xcxapi_get_current_wtype()) {
                $where['campus_id'] = xcxapi_get_current_campus_id();
            } elseif (2 === xcxapi_get_current_wtype() || 3 === xcxapi_get_current_wtype()) {
                $where['campus_id'] = ['in', xcxapi_get_current_campus_id()];
            }
            if (5 === xcxapi_get_current_wtype()) {  //员工级别
                $where['contractor'] = xcxapi_get_current_user_id();
            }

            //今日签单金额
            $where_amount_today['signing_date'] = $date;
            if (4 === xcxapi_get_current_wtype() || 5 === xcxapi_get_current_wtype()) {
                $where_amount_today['campus_id'] = xcxapi_get_current_campus_id();
            } elseif (2 === xcxapi_get_current_wtype() || 3 === xcxapi_get_current_wtype()) {
                $where_amount_today['campus_id'] = ['in', xcxapi_get_current_campus_id()];
            }

            if (5 === xcxapi_get_current_wtype()) {  //员工级别
                $where_amount_today['contractor'] = xcxapi_get_current_user_id();
            }

            //今日跟进客户
            $where_follow['next_follow_up_record'] = $date;
            if (4 === xcxapi_get_current_wtype() || 5 === xcxapi_get_current_wtype()) {
                $where_follow['campus_id'] = xcxapi_get_current_campus_id();
            } elseif (2 === xcxapi_get_current_wtype() || 3 === xcxapi_get_current_wtype()) {
                $where_follow['campus_id'] = ['in', xcxapi_get_current_campus_id()];
            }

            if (5 === xcxapi_get_current_wtype()) {  //员工级别
                $where_follow['user_id'] = xcxapi_get_current_user_id();
            }

            //今日新增用户
            $where_newly_added['create_time'] = ['between', [$start, $end]];
            if (4 === xcxapi_get_current_wtype() || 5 === xcxapi_get_current_wtype()) {
                $where_newly_added['campus_id'] = xcxapi_get_current_campus_id();
            } elseif (2 === xcxapi_get_current_wtype() || 3 === xcxapi_get_current_wtype()) {
                $where_newly_added['campus_id'] = ['in', xcxapi_get_current_campus_id()];
            }

            if (5 === xcxapi_get_current_wtype()) {  //员工级别
                $where_newly_added['user_id'] = xcxapi_get_current_user_id();
            }

            $month_day = $this->getthemonth($date);
            $start_month_day = $month_day[0]; //本月第一天
            $end_month_day = $month_day[1];  //本月最后一天

            //本月签单数量
            $where_month_register['signing_date'] = ['between', [$start_month_day, $end_month_day]];
            if (4 === xcxapi_get_current_wtype() || 5 === xcxapi_get_current_wtype()) {
                $where_month_register['campus_id'] = xcxapi_get_current_campus_id();
            } elseif (2 === xcxapi_get_current_wtype() || 3 === xcxapi_get_current_wtype()) {
                $where_month_register['campus_id'] = ['in', xcxapi_get_current_campus_id()];
            }

            if (5 === xcxapi_get_current_wtype()) {  //员工级别
                $where_month_register['contractor'] = xcxapi_get_current_user_id();
            }

            //本月签单金额
            $where_amount_month['signing_date'] = ['between', [$start_month_day, $end_month_day]];
            if (4 === xcxapi_get_current_wtype() || 5 === xcxapi_get_current_wtype()) {
                $where_amount_month['campus_id'] = xcxapi_get_current_campus_id();
            } elseif (2 === xcxapi_get_current_wtype() || 3 === xcxapi_get_current_wtype()) {
                $where_amount_month['campus_id'] = ['in', xcxapi_get_current_campus_id()];
            }

            if (5 === xcxapi_get_current_wtype()) {  //员工级别
                $where_amount_month['contractor'] = xcxapi_get_current_user_id();
            }

            //欠费人数
            $where_student = [];
            if (4 === xcxapi_get_current_wtype() || 5 === xcxapi_get_current_wtype()) {
                $where_student['campus_id'] = xcxapi_get_current_campus_id();
            } elseif (2 === xcxapi_get_current_wtype() || 3 === xcxapi_get_current_wtype()) {
                $where_student['campus_id'] = ['in', xcxapi_get_current_campus_id()];
            }
            $where_student['arrears_status'] = 0;

            if (5 === xcxapi_get_current_wtype()) {  //员工级别
                $where_student['user_id'] = xcxapi_get_current_user_id();
            }

            //校区业绩
            $where_campus = [];
            $allocationcampus = [];
            if (4 === xcxapi_get_current_wtype() || 5 === xcxapi_get_current_wtype()) {
                $where_campus['id'] = xcxapi_get_current_campus_id();
            } elseif (2 === xcxapi_get_current_wtype() || 3 === xcxapi_get_current_wtype()) {
                $where_campus['id'] = ['in', xcxapi_get_current_campus_id()];
            }
            $where_campus['status'] = 1;
            $campus_result = (new CampusModel())->where($where_campus)->select();
            $achievement_campus_amount = 0;
            $achievement_campus_name = '暂无';
            $allocationcampus['create_time'] = ['between', [$start_month_day, $end_month_day]];
            foreach ($campus_result as $key => $value) {
                $allocationcampus['campus_id_all'] = $value['id'];
                $campus_money = (new AllocationCampusModel())->where($allocationcampus)->sum('rate_all_money'); //校区业绩
                if ($campus_money > $achievement_campus_amount) {
                    $achievement_campus_amount = $campus_money;
                    $achievement_campus_name = $value['campus_name'];
                }
            }
            $achievement_campus_amount = ($achievement_campus_amount / 100);  //校区业绩金额

            //个人业绩
            $where_only = [];
            $only_name = (new UserModel())->where(['company_id' => xcxapi_get_current_company_id()])->select();
            $achievement_only_amount = 0;
            $achievement_only_name = '暂无';
            $where_only['create_time'] = ['between', [$start_month_day, $end_month_day]];
            foreach ($only_name as $key => $value) {
                $where_only['payee_only'] = $value['id'];
                if (4 === xcxapi_get_current_wtype() || 5 === xcxapi_get_current_wtype()) {
                    $where_only['campus_id'] = xcxapi_get_current_campus_id();
                } elseif (2 === xcxapi_get_current_wtype() || 3 === xcxapi_get_current_wtype()) {
                    $where_only['campus_id'] = ['in', xcxapi_get_current_campus_id()];
                }
                $only_money = (new AllocationOnlyModel())->where($where_only)->sum('rate_only_money'); //个人业绩
                if ($only_money > $achievement_only_amount) {
                    $achievement_only_amount = $only_money;
                    $achievement_only_name = $value['name'];
                }
            }
            $achievement_only_amount = ($achievement_only_amount / 100);  //个人业绩金额

            //当前账号的业绩
            $current_only_sum_money = (new AllocationOnlyModel())->where(['payee_only' => xcxapi_get_current_user_id()])->sum('rate_only_money'); //个人业绩
            $current_only_sum_money = ($current_only_sum_money / 100);

            //今日签单
            $this->data['today_signing_num'] = (new ContractModel())->where($where)->count();
            //今日签单金额
            $this->data['today_amount_total'] = ((new ContractModel())->where($where_amount_today)->sum('total_amount') / 100); //总额
            //今日跟进
            $this->data['today_follow'] = (new StudentModel())->where($where_follow)->count();
            //今日新增
            $this->data['today_newly_added'] = (new StudentModel())->where($where_newly_added)->count();
            //本月签单数
            $this->data['month_num'] = (new ContractModel())->where($where_month_register)->count();
            //本月签单金额
            $this->data['month_amount_total'] = ((new ContractModel())->where($where_amount_month)->sum('total_amount') / 100); //总额
            //欠费人数
            $this->data['arrears_num'] = (new StudentModel())->where($where_student)->count();
            //校区业绩名称
            $this->data['achievement_campus_name'] = $achievement_campus_name;
            //校区业绩总额
            $this->data['achievement_campus_amount'] = $achievement_campus_amount;
            //个人业绩名称
            $this->data['achievement_only_name'] = $achievement_only_name;
            //个人业绩总额
            $this->data['achievement_only_amount'] = $achievement_only_amount;
            //当前个人业绩总额
            $this->data['current_only_sum_money'] = $current_only_sum_money;
        } catch (\Exception $e) {
            $this->errorMessage($e, $params);
        }

        return $this->return;
    }

    /**
     * User: chenben
     * Date: 2020/03/09.
     *获取当月第一天和最后一天.
     *
     * @param $date
     *
     * @return array
     */
    protected function getthemonth($date)
    {
        $firstday = date('Y-m-01', strtotime($date));
        $lastday = date('Y-m-d', strtotime("${firstday} +1 month -1 day"));

        return [$firstday, $lastday];
    }


	/**
	 * User: wufeng
	 * Date: 2020/03/12.
	 *
	 * @param $params
	 *
	 * @return array
	 */
	public function information($params)
	{
		try {
			$this->data['systemNoticeList'] = (new MyNoticeObjectModel())
				->where(['notice_object_id' => xcxapi_get_current_user_id()])
				->order('id desc')
				->select()
				->toArray();
			$is_unread_count = 0;
			foreach ($this->data['systemNoticeList'] as &$val) {
				$myNotice = (new MyNoticeModel())->get($val['notice_id']);
				$val['notice_name'] = $myNotice['notice_name'];
				$val['notice_time'] = $myNotice['notice_time'];
				$val['user_id'] = $myNotice['user_id'];
				$val['user_name'] = (new UserModel())->get($myNotice['user_id'])['name'];
				$val['content'] = $myNotice['content'];
				$val['audio'] = $myNotice['audio'];
				$val['audio_duration'] = $myNotice['audio_duration'];
				$val['video'] = $myNotice['video'];
				$val['picture'] = explode(',', $myNotice['picture']) ? explode(',', $myNotice['picture']) : [];
				$val['s_notice_type'] = $val['notice_type'];
				$val['status'] = $myNotice['status'];
				if($val['is_read'] == 0){ //未读
					$is_unread_count = $is_unread_count + 1;
				}
			}
			$this->data['systemNoticeUnReadCount'] = $is_unread_count;
			if (empty($this->data)) {
				/*throw new BaseException('NO_DATA');*/
			}
		} catch (\Exception $e) {
			$this->errorMessage($e, $params);
		}

		return $this->return;
	}

	/**
	 * User: wufeng
	 * Date: 2020/03/14.
	 *
	 * @param $params
	 *
	 * @return array
	 */
	public function isRead($params)
	{
		try {
			(new MyNoticeObjectModel())->save(['is_read' => 1], ['id' => $params['id']]);
		} catch (\Exception $e) {
			$this->errorMessage($e, $params);
		}

		return $this->return;
	}

	/**
	 * User: wufeng
	 * Date: 2020/03/30
	 *
	 * @param $params
	 * @return array
	 */
	public function uploadHead($params)
	{
		try {
			(new UserModel())->save(['img' => $params['img']], ['id' => $params['id']]);
		} catch (\Exception $e) {
			$this->errorMessage($e, $params);
		}

		return $this->return;
	}

	/**
	 * User: wufeng
	 * Date: 2020/03/07.
	 *
	 * @param $params
	 *
	 * @return array
	 */
	public function coordination($params)
	{
		try {
			$where = $_where = $own_where = [];
			$approval_id_array =(new MyApprovalModel())->where(['operation_type' => 2])->column('id'); //协同ID
			$approval_id_array = array_unique($approval_id_array);
			$where['approval_id'] = ['in', implode(',',$approval_id_array )];
			$_where['approval_id'] = ['in', implode(',',$approval_id_array )];
			
			$approval_id_own_array =(new MyApprovalModel())->where(['operation_type' => 2, 'user_id' => xcxapi_get_current_user_id()])->column('id'); //协同ID
			$approval_id_own_array = array_unique($approval_id_own_array);
			$own_where['approval_id'] = ['in', implode(',',$approval_id_own_array )];
			$own_coordination_res = (new MyApprovalRelationModel())->where($own_where)->select()->toArray();
			$own_coordination = [];
			$own_coordination_key = [];
			foreach ($own_coordination_res as &$item_val) {
				if(!in_array($item_val['approval_id'], $own_coordination_key)){
					$own_coordination_key[] = $item_val['approval_id'];
					$own_coordination[] = $item_val['id'];
				}
			}
			
			
			$where['status'] = 1;
			$where['approver_id'] = xcxapi_get_current_user_id();
			$this->data['stay_approval'] = (new MyApprovalRelationModel())
				->where($where)
				->order('id desc')
				->select()
				->toArray();
			foreach ($this->data['stay_approval'] as &$datum) {
				$res_approval = (new MyApprovalModel())->get(['id' => $datum['approval_id']]);
				$datum['publisher_name'] = (new UserModel())->get(['id' => $res_approval['user_id']])['name'];
				$datum['content'] = $res_approval['content'];
				$datum['approval_audio'] = $res_approval['audio'];
				$datum['approval_audio_duration'] = $res_approval['audio_duration'];
				$datum['approval_video'] = $res_approval['video'];
				$picture_arr = [];
				if($res_approval['picture'] != null && $res_approval['picture'] != ''){
					$picture_arr = explode(',',$res_approval['picture']);
				}
				$datum['approval_picture'] = $picture_arr;
				
				
				$picture_arr_return = [];
				if($datum['picture'] != null && $datum['picture'] != ''){
					$picture_arr_return = explode(',',$datum['picture']);
				}
				$datum['picture'] = $picture_arr_return;
				
				$datum['coordination_time'] = $res_approval['coordination_time'];
				$datum['reminder_time'] = $res_approval['reminder_time'];
				$datum['approval_time'] = $res_approval['approval_time'];
				$datum['name'] = (new UserModel())->get($datum['approver_id'])['name'];
				
				
				$approval_res =(new MyApprovalRelationModel())->where(['approval_id' => $datum['approval_id']])->select()->toArray();
				$approval_content_array = [];
				$approval_video_array = [];
				$approval_audio_array = [];
				$approval_picture_array = [];
				foreach ($approval_res as &$item) {
					//内容
					if($item['approval_content'] != '' && $item['approval_content'] != null){
					$name_user = (new UserModel())->get($item['approver_id'])['name'];
					$content_user = $item['approval_content'];
					$str = $name_user.':'.$content_user;
					$approval_content_array[] = $str;
					}
					//视频
					if($item['video'] != '' && $item['video'] != null){
						$cell_item = [];
						$cell_item['name'] = $name_user;
						$cell_item['video'] = $item['video'];
						$approval_video_array[] = $cell_item;
					}
					
					//音频
					if($item['audio'] != '' && $item['audio'] != null){
						$cell_item = [];
						$cell_item['name'] = $name_user;
						$cell_item['audio_duration'] = $item['audio_duration'];
						$cell_item['audio'] = $item['audio'];
						$approval_audio_array[] = $cell_item;
					}
					//照片
					if($item['picture'] != '' && $item['picture'] != null){
						$cell_item = [];
						$cell_item['name'] = $name_user;
						$cell_item['picture'] = explode(',',$item['picture']);
						$approval_picture_array[] = $cell_item;
					}
				}
				//内容
				$datum['approval_content_array'] = $approval_content_array;
				//视频
				$datum['approval_video_array'] = $approval_video_array;
				//音频
				$datum['approval_audio_array'] = $approval_audio_array;
				//照片
				$datum['approval_picture_array'] = $approval_picture_array;
				
				
				$approver_arr = explode(',',$res_approval['approver']);
				$approver_name = '';
				for($i = 0; $i < count($approver_arr); $i ++){
					$res_approver = (new UserModel())->get(['id' => $approver_arr[$i]]);
					$cell_approver = $res_approver['name'].'/';
					$approver_name = $approver_name.$cell_approver;
				}
				$datum['approver_name'] = $approver_name;
				
				$student_arr = explode(',',$res_approval['students']);
				$student_name_and_phone = '';
				for($i = 0; $i < count($student_arr); $i ++){
					$res_student = (new StudentModel())->get(['id' => $student_arr[$i]]);
					$cell_student = '('.$res_student['student_name'].'/'.$res_student['phone'].')';
					$student_name_and_phone = $student_name_and_phone.$cell_student;
				}
				$datum['student_name_and_phone'] = $student_name_and_phone;
			}

			$_where['status'] = 2;
			$_where['approver_id'] = xcxapi_get_current_user_id();
			$this->data['already_approval'] = (new MyApprovalRelationModel())
				->where($_where)
				->order('id desc')
				->select()
				->toArray();
			foreach ($this->data['already_approval'] as &$datum) {
				$res_approval = (new MyApprovalModel())->get(['id' => $datum['approval_id']]);
				$datum['publisher_name'] = (new UserModel())->get(['id' => $res_approval['user_id']])['name'];
				$datum['content'] = $res_approval['content'];
				$datum['approval_audio'] = $res_approval['audio'];
				$datum['approval_audio_duration'] = $res_approval['audio_duration'];
				$datum['approval_video'] = $res_approval['video'];
				$picture_arr = [];
				if($res_approval['picture'] != null && $res_approval['picture'] != ''){
					$picture_arr = explode(',',$res_approval['picture']);
				}
				$datum['approval_picture'] = $picture_arr;
				
				$picture_arr_return = [];
				if($datum['picture'] != null && $datum['picture'] != ''){
					$picture_arr_return = explode(',',$datum['picture']);
				}
				$datum['picture'] = $picture_arr_return;
				
				$datum['coordination_time'] = $res_approval['coordination_time'];
				$datum['reminder_time'] = $res_approval['reminder_time'];
				$datum['approval_time'] = $res_approval['approval_time'];
				$datum['name'] = (new UserModel())->get($datum['approver_id'])['name'];
				
				$approval_res =(new MyApprovalRelationModel())->where(['approval_id' => $datum['approval_id']])->select()->toArray();
				$approval_content_array = [];
				$approval_video_array = [];
				$approval_audio_array = [];
				$approval_picture_array = [];
				foreach ($approval_res as &$item) {
					//内容
					if($item['approval_content'] != '' && $item['approval_content'] != null){
					$name_user = (new UserModel())->get($item['approver_id'])['name'];
					$content_user = $item['approval_content'];
					$str = $name_user.':'.$content_user;
					$approval_content_array[] = $str;
					}
					//视频
					if($item['video'] != '' && $item['video'] != null){
						$cell_item = [];
						$cell_item['name'] = $name_user;
						$cell_item['video'] = $item['video'];
						$approval_video_array[] = $cell_item;
					}
					
					//音频
					if($item['audio'] != '' && $item['audio'] != null){
						$cell_item = [];
						$cell_item['name'] = $name_user;
						$cell_item['audio_duration'] = $item['audio_duration'];
						$cell_item['audio'] = $item['audio'];
						$approval_audio_array[] = $cell_item;
					}
					//照片
					if($item['picture'] != '' && $item['picture'] != null){
						$cell_item = [];
						$cell_item['name'] = $name_user;
						$cell_item['picture'] = explode(',',$item['picture']);
						$approval_picture_array[] = $cell_item;
					}
				}
				//内容
				$datum['approval_content_array'] = $approval_content_array;
				//视频
				$datum['approval_video_array'] = $approval_video_array;
				//音频
				$datum['approval_audio_array'] = $approval_audio_array;
				//照片
				$datum['approval_picture_array'] = $approval_picture_array;
				
				
				$approver_arr = explode(',',$res_approval['approver']);
				$approver_name = '';
				for($i = 0; $i < count($approver_arr); $i ++){
					$res_approver = (new UserModel())->get(['id' => $approver_arr[$i]]);
					$cell_approver = $res_approver['name'].'/';
					$approver_name = $approver_name.$cell_approver;
				}
				$datum['approver_name'] = $approver_name;
				
				$student_arr = explode(',',$res_approval['students']);
				$student_name_and_phone = '';
				for($i = 0; $i < count($student_arr); $i ++){
					$res_student = (new StudentModel())->get(['id' => $student_arr[$i]]);
					$cell_student = '('.$res_student['student_name'].'/'.$res_student['phone'].')';
					$student_name_and_phone = $student_name_and_phone.$cell_student;
				}
				$datum['student_name_and_phone'] = $student_name_and_phone;
			}
			
			
			
		
			$this->data['own_approval'] = (new MyApprovalRelationModel())
				->where(['id' => ['in', implode(',',$own_coordination )]])
				->order('id desc')
				->select()
				->toArray();
			foreach ($this->data['own_approval'] as &$datum) {
				$res_approval = (new MyApprovalModel())->get(['id' => $datum['approval_id']]);
				$datum['publisher_name'] = (new UserModel())->get(['id' => $res_approval['user_id']])['name'];
				$datum['content'] = $res_approval['content'];
				$datum['approval_audio'] = $res_approval['audio'];
				$datum['approval_audio_duration'] = $res_approval['audio_duration'];
				$datum['approval_video'] = $res_approval['video'];
				$picture_arr = [];
				if($res_approval['picture'] != null && $res_approval['picture'] != ''){
					$picture_arr = explode(',',$res_approval['picture']);
				}
				$datum['approval_picture'] = $picture_arr;
				
				$picture_arr_return = [];
				if($datum['picture'] != null && $datum['picture'] != ''){
					$picture_arr_return = explode(',',$datum['picture']);
				}
				$datum['picture'] = $picture_arr_return;
				
				$datum['coordination_time'] = $res_approval['coordination_time'];
				$datum['reminder_time'] = $res_approval['reminder_time'];
				$datum['approval_time'] = $res_approval['approval_time'];
				$datum['name'] = (new UserModel())->get($datum['approver_id'])['name'];
				
				$approval_res =(new MyApprovalRelationModel())->where(['approval_id' => $datum['approval_id']])->select()->toArray();
				$approval_content_array = [];
				$approval_video_array = [];
				$approval_audio_array = [];
				$approval_picture_array = [];
				$status_approval = 1; //未处理
				foreach ($approval_res as &$item) {
					
					if($item['status'] == 2){  //有一条处理了表示已经处理
						$status_approval = 2;
					}
					
					//内容
					if($item['approval_content'] != '' && $item['approval_content'] != null){
					$name_user = (new UserModel())->get($item['approver_id'])['name'];
					$content_user = $item['approval_content'];
					$str = $name_user.':'.$content_user;
					$approval_content_array[] = $str;
					}
					//视频
					if($item['video'] != '' && $item['video'] != null){
						$cell_item = [];
						$cell_item['name'] = $name_user;
						$cell_item['video'] = $item['video'];
						$approval_video_array[] = $cell_item;
					}
					
					//音频
					if($item['audio'] != '' && $item['audio'] != null){
						$cell_item = [];
						$cell_item['name'] = $name_user;
						$cell_item['audio_duration'] = $item['audio_duration'];
						$cell_item['audio'] = $item['audio'];
						$approval_audio_array[] = $cell_item;
					}
					//照片
					if($item['picture'] != '' && $item['picture'] != null){
						$cell_item = [];
						$cell_item['name'] = $name_user;
						$cell_item['picture'] = explode(',',$item['picture']);
						$approval_picture_array[] = $cell_item;
					}
				}
				//内容
				$datum['approval_content_array'] = $approval_content_array;
				//视频
				$datum['approval_video_array'] = $approval_video_array;
				//音频
				$datum['approval_audio_array'] = $approval_audio_array;
				//照片
				$datum['approval_picture_array'] = $approval_picture_array;
				
				
				$approver_arr = explode(',',$res_approval['approver']);
				$approver_name = '';
				for($i = 0; $i < count($approver_arr); $i ++){
					$res_approver = (new UserModel())->get(['id' => $approver_arr[$i]]);
					$cell_approver = $res_approver['name'].'/';
					$approver_name = $approver_name.$cell_approver;
				}
				$datum['approver_name'] = $approver_name;
				
				$student_arr = explode(',',$res_approval['students']);
				$student_name_and_phone = '';
				for($i = 0; $i < count($student_arr); $i ++){
					$res_student = (new StudentModel())->get(['id' => $student_arr[$i]]);
					$cell_student = '('.$res_student['student_name'].'/'.$res_student['phone'].')';
					$student_name_and_phone = $student_name_and_phone.$cell_student;
				}
				$datum['student_name_and_phone'] = $student_name_and_phone;
				$datum['status_approval'] = $status_approval;
			}
			
		} catch (\Exception $e) {
			$this->errorMessage($e, $params);
		}

		return $this->return;
	}

	/**
	 * User: wufeng
	 * Date: 2020/03/07.
	 *
	 * @param $params
	 *
	 * @return array
	 */
	public function launchCoordination($params)
	{
		try {
			$where = $data = [];
			$where['id'] = $params['id'];
			$data['status'] = 2;
			$data['approval_content'] = $params['approval_content'];
			$data['audio'] = $params['audio'];
			$data['audio_duration'] = $params['audio_duration'];
			$data['video'] = $params['video'];
			$data['picture'] = $params['picture'];
			$data['approval_time'] = date('Y-m-d H:i:s');
			(new MyApprovalRelationModel())->save($data,$where);
		} catch (\Exception $e) {
			$this->errorMessage($e, $params);
		}

		return $this->return;
	}
}
